import sys
import traceback

from xjlibrary.all_step.BaseAllStep import GlobalControl
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
print(curPath)
sys.path.insert(0, curPath)  # 插入

from Step1_downlist import main as step1_main
from Step2_para_list import main as step2_main
from Step3_downbook import main as step3_main

"""
第4步单独运行 因为图片是base64编码的在数据里
第四部会将base64转化为图片
"""


class UseGlobalControl(GlobalControl):

    def __init__(self):
        super().__init__(__file__, BaseDir.get_new_path(BaseDir.get_file_dir_absolute(__file__), "logs"))

    def imports(self):
        # from Step1_downlist import main as step1_main
        # from Step2_para_list import main as step2_main
        # from Step3_downbook import main as step3_main
        pass

    def run(self, strings):
        """
        该步骤运行代码
        :return:
        """
        try:
            if strings == "":
                return True
            result = eval(strings)
        except:
            print(traceback.format_exc())
            return False
        return result

    def start_hook(self, key):
        if key == "3":
            self.key = "3"
            self.nowstep = ""
            return True

    def one_check(self):
        """
        该步骤检查代码是否运行完成
        :return:
        """
        if self.key == "1":
            print("检查第一步")
            from Step1_downlist import sPath
            num = BaseDir.get_dir_file_num(sPath)
            if num == 27:
                print("执行正常")
                self.writetrue()
            else:
                self.writefalse("量不对")
        elif self.key == "2":
            print("检查第二步")
            self.writetrue("没有好的验证机制 默认通过")

        elif self.key == "3":

            sql = "select url,bookname from book where stat=0 and failcount<3 limit 1000"
            rows = self.mysqlutils.SelectFromDB(sql)
            if not rows:
                self.writetrue()
            else:
                self.writefalse("")
        if "3" in self.jsondicts.keys() and self.jsondicts["3"]["isFinish"]:
            return True
        return False


if __name__ == "__main__":
    globalcontrol = UseGlobalControl()
    globalcontrol.work()
